//
//  Login.m
//  Opper
//
//  Created by Le Dung on 4/12/13.
//  Copyright (c) 2013 LeDung. All rights reserved.
//

#import "Login.h"
#import "StyleClass.h"
#import <Twitter/Twitter.h>

@interface Login ()

@end

@implementation Login
@synthesize isLoginRight,isMove;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self buttonStyle];
    [self buttonTarget];
    [self styleContaint];
    [StyleClass createNavi:self andTitle:@"Login"];
    [StyleClass background:self.view];
    [self creatFaceSignUp];
    
    self.eMail.text=self.accountTemp.userTemp;
    self.passWord.text =self.accountTemp.passTemp;
 
}

-(void)styleContaint
{
    self.eMail.delegate=self;
    self.passWord.delegate=self;
    self.eMail.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.eMail.layer.shadowOffset = CGSizeMake(2, 2.0);
    self.eMail.layer.shadowOpacity = 1.0f;
    
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
}



-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
-(void)buttonTarget
{
    [self.loginButton addTarget:self action:@selector(gotoMainScreen) forControlEvents:UIControlEventTouchUpInside];
    [self.loginButton setBackgroundImage:[StyleClass nGreen] forState:UIControlStateNormal];
    
    [self.registerButton addTarget:self action:@selector(gotoRegisterScreen) forControlEvents:UIControlEventTouchUpInside];
    [self.registerButton setBackgroundImage:[StyleClass nGreen] forState:UIControlStateNormal];
    
    [self.eMail setBackground:[StyleClass nText]];
    [self.passWord setBackground:[StyleClass nText]];

}


-(void)creatFaceSignUp
{

    FBLoginView *loginview = [[FBLoginView alloc] init];
    loginview.frame = CGRectMake(10, 200, 300, 40);
    loginview.delegate = self;
    
    for (id obj in loginview.subviews)
    {
        if ([obj isKindOfClass:[UIButton class]])
        {
            UIButton * loginButton =  obj;
            [loginButton setFrame:CGRectMake(0, 0, 300, 40)];
            
            UIImage *loginImage = [UIImage imageNamed:@"FaceBookLogin"];
            [loginButton setImage:loginImage forState:UIControlStateNormal];
            [loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];
            
        }
        if ([obj isKindOfClass:[UILabel class]])
        {
            UILabel * loginLabel =  obj;
            [loginview setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
            [loginLabel setFrame:CGRectMake(0, 0, 0, 0)];
            
        }
    }
    [self.view addSubview:loginview];
    
}



- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
{
    
    if (FBSession.activeSession.isOpen)
    {
        [FBRequestConnection
         startForMeWithCompletionHandler:^(FBRequestConnection *connection,
                                           id<FBGraphUser> user,
                                           NSError *error)
         {
             if (!error)
             {
                 NSString *functionName =@"login";
                 NSMutableDictionary *dictionary =[[[NSMutableDictionary alloc]init] autorelease];
                 [dictionary setValue:@"" forKey:@"email"];
                 [dictionary setValue:@""  forKey:@"password"];
                 [dictionary setValue:user.id forKey:@"facebookid"];
                 NSString *jsonResponse=[GetData sendRequest:functionName andDictionary:dictionary];
                 NSLog(@"jsonrefaceid : %@",jsonResponse);
             }
                 
            }
         ];
    }

}


-(void)checkLogin
{
    NSString *functionName =@"login";
    NSMutableDictionary *dictionary =[[[NSMutableDictionary alloc]init] autorelease];
    [dictionary setValue:self.eMail.text forKey:@"email"];
    [dictionary  setValue:self.passWord.text forKey:@"password"];
    [dictionary  setValue:@"" forKey:@"facebookid"];
    NSString *jsonRespones =[GetData sendRequest:functionName andDictionary:dictionary];

    if(jsonRespones)
    {
        if(![GetData checkError:jsonRespones])
        {
            self.isLoginRight=1;
            [GetData login:jsonRespones] ;
            [GetData App].userAccount.password=self.passWord.text;
        }
        else
        {
            NSString *massage =[GetData checkMassage:jsonRespones];
            UIAlertView *alerView =[[UIAlertView alloc]
                                    initWithTitle:@"Error" message:massage delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil ];
            [alerView show];
            [alerView release];
        }

    }
}

-(void)gotoMainScreen
{
    [self checkLogin];
    if(isLoginRight)
    {
        [FBSession.activeSession closeAndClearTokenInformation];
        [GetData getListData];
        [GetData getListProfile];
            
    MainScreen   *page =[[[MainScreen alloc]init]autorelease];
    [StyleClass currentScreen:self andDestination:page andHideBackButton:YES];
    }
}

-(void)gotoRegisterScreen
{
    Register   *page =[[[Register alloc]init]autorelease];
    [StyleClass currentScreen:self andDestination:page andHideBackButton:YES];
    [FBSession.activeSession closeAndClearTokenInformation];

}
         

-(void)buttonStyle
{
    [self.loginButton.titleLabel setFrame:CGRectMake(0, 0, 100, 100)];
    [self.loginButton setTitle:@"Login" forState:UIControlStateNormal];
}

-(void)accountForget
{
    
    NSMutableDictionary *dictionary =[[[NSMutableDictionary alloc]init] autorelease];
    [dictionary setValue:self.eMail.text forKey:@"Username"];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [_loginButton release];
    [_registerButton release];
    [_eMail release];
    [_passWord release];
    [_containView release];
    [super dealloc];
}
@end
